<?php include "lang.php"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <?php
        include "import.php";
        include "../service/bannerService.php";
        $result = findAll();
        $isHasImage = ConnectionManager::getInstance()->getNumRows() > 0;
        ?>
        <script type="text/javascript">
            $(function(){
                $('#banner').bxSlider({
                    pause: 2500,
                    auto: true,
                    displaySlideQty: 1,
                    moveSlideQty: <?php echo $isHasImage ? 1 : 0; ?>,
                    nextImage: '../images/layout/scroll-right.png',
                    prevImage: '../images/layout/scroll-left.png'
                });
            });
        </script>
        <title>Gia An</title>
    </head>
    <body onunload="unloadP('productPageScroll')" onload="loadP('productPageScroll')">
        <?php include("floating-panel.php"); ?>
        <div class="wrapper">
            <?php include("header.php"); ?>
            <div class="slider">
                <div id="banner">
                    <?php
                    $imagePath = "../images/banner/";
                    while ($row = ConnectionManager::getInstance()->fetch($result)) {
                        echo $row["BANNER_PATH"];
                        ?>
                        <div><img src="<?php echo $imagePath . $row["BANNER_PATH"]; ?>" width="880" height="340"/></div>
                        <?php
                    }
                    if (!$isHasImage) {
                        ?>
                        <div><img src="<?php echo "../images/cat/noimg.jpg"; ?>" width="880" height="340"/></div>
                        <?php
                    }
                    ?>
                </div>
            </div>
            <div class="main_body">
                <?php include("left_menu.php"); ?>

                <div class="main_content">
                    <div class="main_content_header"><?php echo $langs["home.topSellingProducts"]; ?></div>				
                    <?php
                    include("../action/productAction.php");

                    $current_page = isset($_REQUEST['page']) ? $_REQUEST['page'] : 1;

                    $row_per_page = 9;

                    $productCount = countTopSellingProducts();
                    if ($productCount > 0) {
                        $total_page = ceil($productCount / $row_per_page);
                        $start_position = ($current_page * $row_per_page) - $row_per_page;

                        $products = getTopSellingProducts($start_position, $row_per_page);
                    } else {
                        $productCount = countAllProduct('');
                        $productCount = $productCount > 18 ? 18 : $productCount;

                        $total_page = ceil($productCount / $row_per_page);
                        $start_position = ($current_page * $row_per_page) - $row_per_page;

                        $products = getProductOfCategory(0, $start_position, $row_per_page, '');
                    }

                    while ($row = mysql_fetch_array($products)) {
                        buildProductBoxForHomePage($row, $langs);
                    }
                    ?>

                    <div class='paging'>
                        <span class="prn">&lt;&lt;</span>
                        <?php
                        for ($i = 1; $i <= $total_page; $i++) {
                            if ($i == $current_page) {
                                echo "<span>" . $i . "</span>";
                            } else {
                                echo "<a href='index.php?page=" . $i . "'>" . $i . "</a>";
                            }
                        }
                        ?>			
                        <span class="prn">&gt;&gt;</span>
                    </div>
                </div>
            </div>
        </div>
<?php
include("footer.php");
ConnectionManager::getInstance()->closeConnection();
?>
    </body>
</html>
